package com.diandian.backend.client.service;

import com.diandian.backend.client.domain.Permission;
import com.diandian.backend.client.dto.*;
import com.diandian.backend.client.mapper.PermissionMapper;
import com.diandian.backend.client.resp.ClientClientPermissionListResp;
import com.diandian.backend.client.resp.ClientPermissionPageResp;
import com.diandian.backend.client.resp.PermissionDetailResp;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PermissionService {

    @Autowired
    private ClientPermissionService clientPermissionService;

    @Autowired
    private PermissionMapper permissionMapper;

    public PermissionDetailResp getDetail(PermissionDetailParamDTO param) {

        Permission dbData = permissionMapper.getDetail(param);
        if(dbData == null){
            return null;
        }

        PermissionDetailResp result = new PermissionDetailResp();
        result.setId(dbData.getId());
        result.setPermissionGroup(dbData.getPermissionGroup());
        result.setPermissionKey(dbData.getPermissionKey());
        result.setPermissionName(dbData.getPermissionName());
        result.setRemark(dbData.getRemark());
        result.setDel(dbData.getDel());
        return result;
    }

    public PageResult page(ClientPermissionPageParamDTO requestDTO){

        requestDTO.check();
        int pageNum = requestDTO.getPageNum();
        int pageSize = requestDTO.getPageSize();

        // 查询总数
        int totalCount = permissionMapper.getCount(requestDTO);

        // 查询数据集合
        List<Permission> dataList = permissionMapper.getPage(requestDTO, requestDTO.getPageSize(), (requestDTO.getPageNum() - 1) * requestDTO.getPageSize());

        // 组装数据
        List<ClientPermissionPageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                ClientPermissionPageResp item = new ClientPermissionPageResp();
                item.setId(x.getId());
                item.setPermissionGroup(x.getPermissionGroup());
                item.setPermissionKey(x.getPermissionKey());
                item.setPermissionName(x.getPermissionName());
                item.setRemark(x.getRemark());
                item.setDel(x.getDel());
                respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public void add(ClientPermissionAddParamDTO param) {

        // 校验当前分组下是否已存在此权限标识
        PermissionDetailParamDTO permissionQueryDTO = new PermissionDetailParamDTO();
        permissionQueryDTO.setPermissionGroup(param.getPermissionGroup());
        permissionQueryDTO.setPermissionKey(param.getPermissionKey());
        PermissionDetailResp detailResp = getDetail(permissionQueryDTO);
        if(detailResp != null){
            throw new DefaultException("已存在此权限配置，请修改后重试");
        }

        param.setDel(0);
        permissionMapper.add(param);
    }

    public void updateById(ClientPermissionUpdateByIdParamDTO param) {

        param.check();

        // 校验更新记录是否存在
        PermissionDetailParamDTO queryDetailDTO = new PermissionDetailParamDTO();
        queryDetailDTO.setId(param.getId());
        Permission dbData = permissionMapper.getDetail(queryDetailDTO);
        if(dbData == null){
            throw new DefaultException("更新的记录不存在");
        }

        // 已经引用的权限不能修改
        ClientClientPermissionListParamDTO permissionListParamDTO = new ClientClientPermissionListParamDTO();
        List<ClientClientPermissionListResp> relationList = clientPermissionService.getList(permissionListParamDTO);
        if(relationList != null || relationList.size() > 0){
            List<String> permissionIdList = relationList.stream().map(x -> String.valueOf(x.getPermissionId())).collect(Collectors.toList());
            if(permissionIdList.contains(String.valueOf(param.getId()))){
                throw new DefaultException("已经引用的权限不能修改");
            }
        }

        // 校验修改后的记录是否与其他记录重复
        PermissionDetailParamDTO permissionQueryDTO = new PermissionDetailParamDTO();
        permissionQueryDTO.setPermissionGroup(param.getPermissionGroup());
        permissionQueryDTO.setPermissionKey(param.getPermissionKey());
        PermissionDetailResp detailResp = getDetail(permissionQueryDTO);
        if(detailResp != null && !String.valueOf(detailResp.getId()).equals(String.valueOf(param.getId()))){
            throw new DefaultException("已存在此权限配置，请修改后重试");
        }

        permissionMapper.updateById(param);
    }

    public void deleteById(ClientPermissionDeleteByIdParamDTO paramDTO) {

        paramDTO.check();

        // 校验更新记录是否存在
        PermissionDetailParamDTO queryDetailDTO = new PermissionDetailParamDTO();
        queryDetailDTO.setId(paramDTO.getId());
        Permission dbData = permissionMapper.getDetail(queryDetailDTO);
        if(dbData == null){
            throw new DefaultException("删除的记录不存在");
        }

        // 已经引用的权限不能修改
        ClientClientPermissionListParamDTO permissionListParamDTO = new ClientClientPermissionListParamDTO();
        List<ClientClientPermissionListResp> relationList = clientPermissionService.getList(permissionListParamDTO);
        if(relationList != null || relationList.size() > 0){
            List<String> permissionIdList = relationList.stream().map(x -> String.valueOf(x.getPermissionId())).collect(Collectors.toList());
            if(permissionIdList.contains(String.valueOf(paramDTO.getId()))){
                throw new DefaultException("已经引用的权限不能删除");
            }
        }

        permissionMapper.deleteById(paramDTO);
    }
}
